package com.example.librarymanagement.service.impl;

import com.example.librarymanagement.mapper.BookMapper;
import com.example.librarymanagement.model.Book;
import com.example.librarymanagement.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements BookService {

    private final BookMapper bookMapper;

    @Autowired
    public BookServiceImpl(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    @Override
    public List<Book> listBooks() {
        return bookMapper.selectAllBooks();
    }

    @Override
    public Book getBookById(Integer id) {
        return bookMapper.selectBookById(id);
    }

    @Override
    public List<Book> queryBooksByConditions(Map<String, Object> conditions) {
        return bookMapper.selectBooksByConditions(conditions);
    }

    @Override
    public Book addBook(Book book) {
        bookMapper.insertBook(book);
        return book;
    }

    @Override
    public Book updateBook(Integer bookId, Map<String, Object> updates) {
        Book book = bookMapper.selectBookById(bookId);
        if (book == null)
            throw new RuntimeException("Book with ID " + bookId + " not found.");

        for (Map.Entry<String, Object> entry : updates.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            switch (key) {
                case "bookName":
                    book.setBookName((String)value);
                    break;
                case "bookPhoto":
                    book.setBookPhoto((String)value);
                    break;
                case "category":
                    book.setCategory((String)value);
                    break;
                case "author":
                    book.setAuthor((String)value);
                    break;
                case "totalStock":
                    // 确保value是Integer类型，如果不是，则尝试转换
                    if (value instanceof Integer) {
                        book.setTotalStock((Integer)value);
                    } else if (value instanceof String) {
                        try {
                            book.setTotalStock(Integer.parseInt((String)value));
                        } catch (NumberFormatException e) {
                            // 处理转换异常，例如记录日志或抛出自定义异常
                            throw new IllegalArgumentException("Invalid format for totalStock: " + value);
                        }
                    } else {
                        // 如果value既不是Integer也不是String，抛出异常
                        throw new IllegalArgumentException("Unsupported type for totalStock: " + value.getClass().getSimpleName());
                    }
                    break;
                case "description":
                    book.setDescription((String)value);
                    break;
                // 这里不包括 borrowedCount
            }
        }

        bookMapper.updateBookSelective(book);
        return book;
    }

    @Override
    public void deleteBook(Integer bookId) {
        bookMapper.deleteBookById(bookId);
    }

    @Override
    public void updateBookAfterBorrow(Integer bookId) {
        bookMapper.decreaseStockAndIncreaseBorrowCount(bookId);
    }
    @Override
    public void returnBook(Integer bookId) {
        bookMapper.increaseStock(bookId);
    }
    @Override
    public List<Book> searchBooks(Map<String, String> searchCriteria) {
        return bookMapper.searchBooksByCriteria(searchCriteria);
    }
    @Override
    public List<Book> listBorrowRankBooks() {
        return bookMapper.selectBorrowRankBooks();
    }
}